from collections import defaultdict


def main():
    arr = []
    n_rows = int(input())
    n_cols = int(input())
    for i in range(n_rows):
        arr.append([int(x) for x in input().split()])
    assert len(arr) == n_rows
    assert len(arr[0]) == n_cols

    positions = defaultdict(list)
    for i in range(n_rows):
        for j in range(n_cols):
            positions[arr[i][j]].append((i, j))

    result = [[-1 for _ in range(n_cols)] for _ in range(n_rows)]
    for i in range(n_rows):
        for j in range(n_cols):
            if len(positions[arr[i][j]]) != 1:
                distances = [abs(x-i) + abs(y-j) for (x, y) in positions[arr[i][j]] if (x, y) != (i, j)]
                result[i][j] = min(distances)

    print(result)


if __name__ == "__main__":
    main()
